﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.IO;
using LetiLib.Security;
using LetiLib.Connection;
using LetiLib.Data;

namespace LetiLib.Device
{
    class Sender
    {
        public static bool vSendPingCmd(Socket handler)
        {
            // Create buffer
            byte[] btSend = new byte[2];

            btSend[0] = (byte)TAG.SERVER_TAG;
            btSend[1] = (byte)TAG.PING;

            // send buffer data
            return SendBytes(handler, btSend);
        }

        public static bool vSendAckCmd(Socket handler)
        {
            // Create buffer
            byte[] btSend = new byte[2];

            btSend[0] = (byte)TAG.SERVER_TAG;
            btSend[1] = (byte)TAG.ACK;

            // send buffer data
            return SendBytes(handler, btSend);
        }

        public static bool vSendNAckCmd(Socket handler)
        {
            // CreatePageData Buffer
            byte[] btSend = new byte[2];

            btSend[0] = (byte)TAG.SERVER_TAG;
            btSend[1] = (byte)TAG.NACK;

            // send buffer data
            return SendBytes(handler, btSend);
        }

        public static bool vSendSetVersionCmd(Socket handler)
        {
            // create buffer
            byte[] btSend = new byte[5];
            btSend[0] = (byte)TAG.SERVER_TAG;
            btSend[1] = (byte)TAG.SET_VERSION;
            // hard code 101
            btSend[2] = 0x01;
            btSend[3] = 0x00;
            btSend[4] = 0x01;
            // send buffer data
            return SendBytes(handler, btSend);
        }

        public static bool vSendGetVersionCmd(Socket handler)
        {
            byte[] btSend = new byte[2];
            btSend[0] = (byte)TAG.SERVER_TAG;
            btSend[1] = (byte)TAG.GET_VERSION;

            // send buffer data
            return SendBytes(handler, btSend);
        }

        // CMDs sent to Mobiles
        public static bool vSendGetPassCmd(Socket handler)
        {
            // create buffer
            byte[] btSend = new byte[2];
            btSend[0] = (byte)TAG.SERVER_TAG;
            btSend[1] = (byte)TAG.GET_PASSWORD;

            // send buffer data
            return SendBytes(handler, btSend);
        }

        // Update data commands
        public static bool vSendUpdatePlaylistRequest(Socket handler)
        {
            // create buffer
            byte[] data = new byte[2];
            data[0] = (byte)TAG.SERVER_TAG;
            data[1] = (byte)TAG.CMD_SERVER_PLAYLIST_UPDATE_REQ;
            
            // send buffer data
            return SendBytes(handler, data);
        }
        public static bool vSendPlaylistInfo(Socket handler, PlaylistInfo plInfo)
        {
            // send buffer data
            return SendBytes(handler,plInfo.Data);
            
        }

        // Update data commands
        public static bool vSendUpdateDataRequest(Socket handler, Int16 version)
        {
            byte[] data = new byte[2];
            data[0] = (byte)TAG.SERVER_TAG;
            data[1] = (byte)TAG.CMD_SERVER_DATA_UPDATE_REQ;
            
            // send buffer data
            return SendBytes(handler, data);
        }

        public static bool vSendUpdateDataInfo(Socket handler, DataInfo dataInfo)
        {
            // send buffer data
            return SendBytes(handler,dataInfo.Data);
        }

        static int iTmpData = 0;
        public static bool vSendUpdateDataPackage(Socket handler, DataPackage package)
        {
            iTmpData++;

            byte crc = CRC.ComputeChecksum(package.Data);
            byte[] outBuf = new byte[package.Data.Length + 1];
            Array.Copy(package.Data, 0, outBuf, 0, package.Data.Length);
            if (iTmpData % 10 == 0) crc++;
            outBuf[package.Data.Length] = crc;
            // send buffer data
            return SendBytes(handler, outBuf);

        }

        public static bool vSendUpdateFirmwareRequest(Socket handler)
        {
            byte[] data = new byte[2];
            data[0] = (byte)TAG.SERVER_TAG;
            data[1] = (byte)TAG.CMD_SERVER_FIRMWARE_UPDATE_REQ;

            // send buffer data
            return SendBytes(handler, data);
        }

        public static bool vSendUpdateFirmwareInfo(Socket handler, FirmwareInfo frmInfo)
        {
            // send buffer data
            return SendBytes(handler, frmInfo.Data);
        }

        static int iTmp = 0;
        public static bool vSendUpdateFirmwarePackage(Socket handler, FirmwarePackage package)
        {
            iTmp++;

            byte crc = CRC.ComputeChecksum(package.Data);
            byte[] outBuf = new byte[package.Data.Length + 1];
            Array.Copy(package.Data, 0, outBuf, 0, package.Data.Length);
            if (iTmp % 10 == 0) crc++;
            outBuf[package.Data.Length] = crc;

            // send buffer data
            return SendBytes(handler, outBuf);
        }

        public static bool SendCreateBlankPageCmd(Socket handler,ushort mID)
        {            
            LETIPage pg = new LETIPage(mID);

            byte[] data = new byte[2 +pg.BufferSize];
            data[0] = (byte)TAG.SERVER_TAG;
            data[1] = (byte)TAG.CMD_CREATE_PAGE;

            pg.getBytes().CopyTo(data, 2);

            // send buffer data
            return SendBytes(handler, data);
        }

        public static void CreatePageData(Socket handler, LETIPage pageData)
        {

        }
        public static void UpdatePageData(int PageID, LETIPage pageData) { }
        public static void DeletePageData(int PageID) { }
        public static void GetPageInfo(int PageID) { }
        public static void SetActivePage(int PageID) { }
        public static void GetActivePage() { }

        public static bool UpdateBusData(Socket handler,string strContent)
        {
            byte[] data = new byte[4 + strContent.Length];
            data[0] = (byte)TAG.SERVER_TAG;
            data[1] = (byte)TAG.CMD_UPDATE_BUS_DATA;
            BitConverter.GetBytes((ushort)strContent.Length).CopyTo(data, 2);
            Encoding.ASCII.GetBytes(strContent).CopyTo(data, 4);

            // send buffer data
            return SendBytes(handler, data);
        }

        public static bool SendActiveImage(Socket handler, ushort pageID, ushort popupID, ushort imgID)
        {
            byte[] data = new byte[8];
            data[0] = (byte)TAG.SERVER_TAG;
            data[1] = (byte)TAG.CMD_UPDATE_IMAGE_TO_PLAYLIST;
            BitConverter.GetBytes(pageID).CopyTo(data, 2);
            BitConverter.GetBytes(popupID).CopyTo(data, 4);
            BitConverter.GetBytes(imgID).CopyTo(data, 6);

            // send buffer data
            return SendBytes(handler, data);
        }

        public static bool SendActivePage(Socket handler, ushort pageID)
        {
            byte[] data = new byte[4];
            data[0] = (byte)TAG.SERVER_TAG;
            data[1] = (byte)TAG.CMD_SET_ACTIVE_PAGE;
            BitConverter.GetBytes(pageID).CopyTo(data, 2);

            // send buffer data
            return SendBytes(handler, data);
        }

        public static bool SendCreatePageCmd(Socket handler, LETIPage page)
        {
            byte[] data;
            if (page == null)
            {
                data = new byte[4];
                data[0] = (byte)TAG.SERVER_TAG;
                data[1] = (byte)TAG.CMD_CREATE_PAGE;
                BitConverter.GetBytes(0).CopyTo(data, 2);
            }
            else
            {
                data = new byte[4 + page.BufferSize];
                data[0] = (byte)TAG.SERVER_TAG;
                data[1] = (byte)TAG.CMD_CREATE_PAGE;
                BitConverter.GetBytes((ushort)page.BufferSize).CopyTo(data, 2);
                page.getBytes().CopyTo(data, 4);
            }
            // send buffer data
            return SendBytes(handler, data);
        }

        public static bool SendDeletePage(Socket handler, ushort pageID)
        {
            byte[] data = new byte[4];
            data[0] = (byte)TAG.SERVER_TAG;
            data[1] = (byte)TAG.CMD_DELETE_PAGE;
            BitConverter.GetBytes(pageID).CopyTo(data, 2);

            // send buffer data
            return SendBytes(handler, data);
        }

        public static bool SendBytes(Socket handler,byte[] bytes,ushort nTry = 3)
        {
            int byteSents = 0;
            int temp = nTry;
            while (byteSents != bytes.Length)
            {
                if (temp <= 0)
                    break;
                byteSents = handler.Send(bytes);
                temp--;
            }

            if (temp <= 0)
                return false;
            else return true;
        }


        
    }
}
